package tp;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Home extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField txtUser;
	private JButton ok;
	   Socket socket;
       BufferedReader in;
       PrintWriter out;

	/**
	 * Create the frame.
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	public Home() throws UnknownHostException, IOException {
		
		socket = new Socket("127.0.0.1",6020);
		
		setTitle("Planificateur R\u00E9union Travail");
		setIconImage(Toolkit.getDefaultToolkit().getImage("icone.jpg"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 670, 500);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel1 = new JPanel();
		contentPane.add(panel1, BorderLayout.CENTER);
		panel1.setLayout(null);
		
		JLabel lblUtilisateur = new JLabel("Utilisateur :");
		lblUtilisateur.setBounds(202, 194, 73, 22);
		panel1.add(lblUtilisateur);
		
		txtUser = new JTextField();
		txtUser.setBounds(312, 194, 116, 22);
		panel1.add(txtUser);
		txtUser.setColumns(10);
		
		
		ok = new JButton("OK");
		ok.setBounds(412, 294, 116, 22);
		ok.addActionListener(this);
		panel1.add(ok);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
		
		try {
			
			out = new PrintWriter(socket.getOutputStream());
			out.println(txtUser.getText());
        	out.flush();
        	
			in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
			String message_distant = in.readLine();
	        System.out.println(message_distant);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		
		
	}
}
